﻿using System;
using System.Windows.Markup;
using XEvolve.Core.Runtime;

namespace XEvolve.Presentation.Markup
{
    public class ExtensionsExtension : MarkupExtension
    {
        internal static IExtensionRegistry Registry;

        private readonly object extensionPointIdentifier;

        public ExtensionsExtension(object extensionPointIdentifier)
        {
            this.extensionPointIdentifier = extensionPointIdentifier;
        }

        public Type Type { get; set;}

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var extensions = Registry.GetExtensionPoint(extensionPointIdentifier).Extensions;
            return Type == null ? extensions : CreateConcerteTypeArray(extensions);
        }

        private object CreateConcerteTypeArray(object[] values)
        {
            var parameterValue = Array.CreateInstance(Type, values.Length);
            values.CopyTo(parameterValue, 0);
            return parameterValue;
        }
    }
}
